/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.upms.server.service.impl;

import com.jfinal.kit.JsonKit;
import com.lambkit.Lambkit;
import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.common.service.LambkitService;
import com.lambkit.common.util.RequestUtils;
import com.lambkit.module.upms.model.UpmsLogModel;
import com.lambkit.module.upms.server.service.*;
import com.lambkit.web.LambkitResult;
import com.lambkit.common.service.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2018-12-26
 * @version 1.0
 * @since 1.0
 */
@Service
@LambkitService
public class UpmsLogModelServiceImpl extends LambkitModelServiceImpl<UpmsLogModel> implements UpmsLogModelService {
	
	private UpmsLogModel DAO = null;
	
	public UpmsLogModel dao() {
		if(DAO==null) {
			DAO = Lambkit.singleton(UpmsLogModel.class);
		}
		return DAO;
	}
	
	@Override
	public UpmsLogModel addRegistLog(String username, LambkitResult result, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLogModel log = new UpmsLogModel();
		//log.setDescription("用户注册-" + result.getCode() + "-" + result.getMessage());
		if(result.getCode()==1) {
			log.setDescription("注册成功");
		} else {
			log.setDescription("注册失败");
		}
		log.setUri("regist");
		log.setPermissions("regist");
		log.setResult(JsonKit.toJson(result));
		toSave(log, username, request);
		return log;
	}

	@Override
	public UpmsLogModel addLoginLog(String username, LambkitResult result, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLogModel log = new UpmsLogModel();
		//log.setDescription("用户登录-" + result.getCode() + "-" + result.getMessage());
		if(result.getCode()==1) {
			log.setDescription("登录成功");
		} else {
			log.setDescription("登录失败");
		}
		log.setUri("login");
		log.setPermissions("login");
		log.setResult(JsonKit.toJson(result));
		toSave(log, username, request);
		return log;
	}
	
	private boolean toSave(UpmsLogModel log, String username, HttpServletRequest request) {
		if(request!=null) {
			log.setUrl(request.getRequestURI());
			log.setUserAgent(RequestUtils.getUserAgent(request));
			log.setMethod(request.getMethod());
			log.setIp(RequestUtils.getIpAddress(request));
			log.setBasePath(RequestUtils.getBasePath(request));
			log.setParameter(request.getQueryString());
			request.setAttribute("upmsLogModelForControllerAction", log);
		}
		log.setUsername(username);
		//log.setStartTime(DateTimeUtils.getCurrentTimeLong());
		return log.save();
	}

	@Override
	public UpmsLogModel addLogoutLog(String username, LambkitResult result, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLogModel log = new UpmsLogModel();
		//log.setDescription("用户退出-" + result.getCode() + "-" + result.getMessage());
		if(result.getCode()==1) {
			log.setDescription("退出成功");
		} else {
			log.setDescription("退出失败");
		}
		log.setUri("logout");
		log.setResult(JsonKit.toJson(result));
		log.setPermissions("logout");
		toSave(log, username, request);
		return log;
	}

	@Override
	public UpmsLogModel addLog(String username, String desc, String uri, String result, String permissions, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLogModel log = new UpmsLogModel();
		log.setDescription(desc);
		log.setUri(uri);
		log.setPermissions(permissions);
		log.setResult(result);
		toSave(log, username, request);
		return log;
	}

	@Override
	public UpmsLogModel addLog(String username, String desc, String uri, LambkitResult result, String permissions, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLogModel log = new UpmsLogModel();
		log.setDescription(desc + "-" + result.getCode() + "-" + result.getMessage());
		log.setUri(uri);
		log.setPermissions(permissions);
		log.setResult(JsonKit.toJson(result));
		toSave(log, username, request);
		return log;
	}
}
